#include "command.h"

void command_mkdir(command_t *ct, int netFD, MYSQL *conn, client_t *cl){

    char buf[1024] = { 0 };
    if(strcmp(cl->dir_path, "/") == 0){
        sprintf(buf, "%s%s", cl->dir_path, ct->path);
    }else{
        sprintf(buf, "%s/%s", cl->dir_path, ct->path);
    }

    char buf1[2048] = { 0 };
    sprintf(buf1, "select id from file where path = '%s' and father_id = %d", buf, cl->file_id);
    printf("%s\n", buf1);
    int num = mysql_select_file_is_exist(conn, buf1);
    if(num == -1 || num == 1){
        printf("2222222\n");
        //-1,语句错误
        //1，已经存在
        int ret = -1;
        int n = send(netFD, &ret, sizeof(ret), MSG_NOSIGNAL);
        ERROR_CHECK(n, -1, "send_commandMkdir.c");
        return;
    }
    printf("111111111\n");
    //不存在
    bzero(buf1, sizeof(buf1));
    sprintf(buf1, "insert into file (status, file_name, user_name, father_id, path, file_type, md5, sha1)"
            "values(1, '%s', '%s', %d, '%s', 'd', null, null)", ct->path, cl->name, cl->file_id, buf);
    if(mysql_query(conn, buf1)){
         printf("%s\n", mysql_error(conn));
         exit(0);
    }

    int ret = 1;
    send(netFD, &ret, sizeof(ret), MSG_NOSIGNAL);

}
